package io.rainforest.rss.task;

import io.rainforest.rss.client.vo.follow.RssFollowReq;
import io.rainforest.rss.dao.po.RssFollow;
import io.rainforest.rss.service.rssfollow.FollowService;
import io.rainforest.rss.service.rssmain.RssMainService;
import io.rainforest.rss.web.rssmain.RssInitController;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import java.util.List;

/**
 * 定时读取rss订阅列表
 * @author r0ad
 * @version 1.0
 */
@Component
@Slf4j
public class AddFetchJob {
    @Autowired
    private FollowService followService;
    @Autowired
    private RssInitController rssInitController;

    /**
     * 定时刷新rss列表
     */
    @Scheduled(fixedDelay = 10000)
    public void refreshRssList(){
        log.info("开始执行rss订阅更新任务！");
        List<RssFollow> rssFollows = followService.queryList(new RssFollowReq());
        rssFollows.forEach(item->{
            if(!ObjectUtils.isEmpty(item)){
                rssInitController.addFetch(item.getRssId());
            }
        });
        log.info("结束执行rss订阅更新任务！");
    }

}
